package com.epfresh.adapter;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.helper.ApplicationHelper;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.bean.SecondKillTime;

import java.util.List;

import multitype.OnRecyclerViewItemClickListener;

public class SecKillTitleAdapter extends RecyclerView.Adapter<SecKillTitleAdapter.ViewHolder> implements View.OnClickListener {

    private List<SecondKillTime.SeckillTimeBean> list;

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    public void setCategory(List<SecondKillTime.SeckillTimeBean> posts) {
        this.list = posts;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_second_title, parent, false);
        view.setOnClickListener(this);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        lp.width = w;
        Log.e("l___width",""+w);
        Log.e("screen_width","+"+  LocalDisplay.SCREEN_WIDTH_PIXELS);
        SecondKillTime.SeckillTimeBean bean = list.get(position);
        holder.tv_title.setText(bean.getTitle());
        holder.tv_title_child.setTag(R.id.item_key_position,position);
        holder.tv_title.setTag(R.id.item_key_position,position);
        holder.tv_title_child.setOnClickListener(this);
        holder.tv_title.setOnClickListener(this);
        if(bean.getStatus()==1){///3 已结束 2进行中 1即将开始
            holder.tv_title_child.setText("即将开始");
        }else if(bean.getStatus()==2){
            holder.tv_title_child.setText("进行中");
        }else{
            holder.tv_title_child.setText("已结束");
        }
        if (bean.getSelectNumber() == position){
            holder.tv_title.setTextSize(16);
/*
            holder.tv_title_child.setTextSize(11);
*/
            holder.tv_title.setTextColor(Color.parseColor("#4CB18E"));
            holder.tv_title_child.setTextColor(Color.parseColor("#4CB18E"));
            holder.tv_title.setEnabled(false);
        }else {
            holder.tv_title.setTextSize(15);
/*
            holder.tv_title_child.setTextSize(10);
*/
            holder.tv_title.setTextColor(Color.parseColor("#626262"));
            holder.tv_title_child.setTextColor(Color.parseColor("#929292"));
            holder.tv_title.setEnabled(true);
        }
    }


    @Override public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer){
            int position = (int) tag;
            list.get(position).setSelectNumber(position);
            notifyDataSetChanged();
            if (onRecyclerViewItemClickListener != null && position < list.size()){
                onRecyclerViewItemClickListener.onItemClick(v,position, list.get(position));
            }
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv_title;
        TextView tv_title_child;

        ViewHolder(final View itemView) {
            super(itemView);
           tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_title_child = (TextView) itemView.findViewById(R.id.tv_title_child);
        }
    }

    int w;
    public void notifyDataChanged(){
        if (list != null && list.size() > 0){
            int size = list.size();
            w = LocalDisplay.SCREEN_WIDTH_PIXELS;

            if(size == 1){
                w = LocalDisplay.SCREEN_WIDTH_PIXELS;
            }else if (size == 2){
                w /= 2;
            }else {
                w /= 3;
            }
        }
        notifyDataSetChanged();
    }

}
